package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.GradeMajorSubjectBindApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.domain.entity.GradeMajorSubjectBindEntity;
import com.example.wisdomclassroomapi.facade.assembler.GradeMajorSubjectBindFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.GradeMajorSubjectBindRequestDto;
import com.example.wisdomclassroomapi.facade.dto.GradeMajorSubjectBindResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-19
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-19
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/gradeMajorSubjectBind")
@Slf4j
public class GradeMajorSubjectBindController extends BaseController {
    @Resource
    private GradeMajorSubjectBindApplicationService gradeMajorSubjectBindApplicationService;

    @Resource
    private GradeMajorSubjectBindFacadeAssembler gradeMajorSubjectBindFacadeAssembler;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public PageDto<List<GradeMajorSubjectBindResponseDto>> getGradeMajorSubjectBindByPage(@RequestBody @Valid GradeMajorSubjectBindRequestDto request) {
        IPage<GradeMajorSubjectBindEntity> page = new Page<GradeMajorSubjectBindEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
        GradeMajorSubjectBindEntity entity = gradeMajorSubjectBindFacadeAssembler.assembleGradeMajorSubjectBindEntity(request);
        page = gradeMajorSubjectBindApplicationService.page(entity, page);
        List<GradeMajorSubjectBindResponseDto> assembleData = new ArrayList<>();
        if (page != null && page.getRecords() != null && page.getRecords().size() > 0) {
            for (GradeMajorSubjectBindEntity item : page.getRecords()) {
                assembleData.add(gradeMajorSubjectBindFacadeAssembler.assembleGradeMajorSubjectBindResponseDto(item));
            }
        }
        PageDto<List<GradeMajorSubjectBindResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setData(assembleData);
        return result;
    }


    /**
     * 查询
     */
    @PostMapping("/list")
    public List<GradeMajorSubjectBindResponseDto> getGradeMajorSubjectBinds(@RequestBody @Valid GradeMajorSubjectBindRequestDto request) {
        GradeMajorSubjectBindEntity entity = gradeMajorSubjectBindFacadeAssembler.assembleGradeMajorSubjectBindEntity(request);
        List<GradeMajorSubjectBindEntity> list = gradeMajorSubjectBindApplicationService.list(entity);
        List<GradeMajorSubjectBindResponseDto> assembleData = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (GradeMajorSubjectBindEntity item : list) {
                assembleData.add(gradeMajorSubjectBindFacadeAssembler.assembleGradeMajorSubjectBindResponseDto(item));
            }
        }
        return assembleData;
    }

    /**
     * 获取某一实体
     */
    @PostMapping("/getGradeMajorSubjectBind")
    public GradeMajorSubjectBindResponseDto getGradeMajorSubjectBindDetails(String id) {
        GradeMajorSubjectBindEntity entity = gradeMajorSubjectBindApplicationService.findById(id);
        GradeMajorSubjectBindResponseDto responseDto = null;
        if (entity != null) {
            responseDto = gradeMajorSubjectBindFacadeAssembler.assembleGradeMajorSubjectBindResponseDto(entity);
        }
        return responseDto;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public void saveGradeMajorSubjectBind(@RequestBody GradeMajorSubjectBindRequestDto request) {
        GradeMajorSubjectBindEntity entity = gradeMajorSubjectBindFacadeAssembler.assembleGradeMajorSubjectBindEntity(request);
        gradeMajorSubjectBindApplicationService.add(entity);
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    public void modifyGradeMajorSubjectBind(@RequestBody GradeMajorSubjectBindRequestDto request) {
        GradeMajorSubjectBindEntity entity = gradeMajorSubjectBindFacadeAssembler.assembleGradeMajorSubjectBindEntity(request);
        gradeMajorSubjectBindApplicationService.updateById(entity);
    }

    /**
     * 删除数据
     */
    @PostMapping("/delete")
    public void removeAuthUser(@RequestBody Map<String, String> params) {
        gradeMajorSubjectBindApplicationService.deleteById(params.get("id"));
    }
}